import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

import unittest
import Image
import array
import img_utils as iu

class TestCrop(unittest.TestCase):
    # reference detection plus crop behavoir
    
    def setUp(self):
        self.ref_color = (255,0,0,255)
        self.ot_color1 = (0,255,0,255)
        self.width = 7
        self.height = 5
        # start with image filled with ref_color
        self.im = Image.new('RGBA', (self.width, self.height), self.ref_color)
        
    def test_ref_touch_borders(self):
        rect = iu.get_reference_rect(self.im, self.ref_color)
        self.assertEquals((0, 0, self.width-1, self.height-1), rect)
        im2 = iu.crop(self.im, rect)
        assert im2.size == (self.width, self.height)
        for y in xrange(self.height):
            for x in xrange(self.width):
                self.assertEquals(im2.getpixel((x,y)), self.ref_color)

    def test_ref_not_touch_borders(self):
        mix = [
            [ 0, 0, 0, 0, 0, 0, 0 ],
            [ 0, 0, 1, 1, 0, 0, 0 ],
            [ 0, 0, 1, 1, 0, 0, 0 ],
            [ 0, 0, 0, 0, 0, 0, 0 ],
            [ 0, 0, 0, 0, 0, 0, 0 ]
            ]
        mix_rect = (2, 1, 3, 2)
        mix_size = (2, 2)
        # basic sanity test data
        assert len(mix)==self.height
        for row in mix:
            assert len(row)==self.width
        im = self.im
        # build image
        for y in xrange(self.height):
            for x in xrange(self.width):
                if mix[y][x]:
                    im.putpixel((x,y),self.ref_color)
                else:
                    im.putpixel((x,y),self.ot_color1)

        rect = iu.get_reference_rect(im, self.ref_color)
        self.assertEquals(rect, mix_rect)    
        im2 = iu.crop(im, rect)
        w, h = im2.size
        for y in xrange(h):
            for x in xrange(w):
                self.assertEquals(im2.getpixel((x,y)), self.ref_color)
        self.assertEquals(im2.size, mix_size)

        # crop companion image well behaved
        base_blue = 230
        blue = base_blue
        for y in xrange(self.height):
            for x in xrange(self.width):
                if mix[y][x]:
                    im.putpixel((x,y),(0,0,blue,255))
                    blue += 1
                else:
                    im.putpixel((x,y),self.ot_color1)
        im2 = iu.crop(im, rect)
        w, h = im2.size
        blue = base_blue
        for y in xrange(h):
            for x in xrange(w):
                self.assertEquals(im2.getpixel((x,y)), (0,0,blue,255))
                blue += 1

class TestCombineArrays(unittest.TestCase):
    def test_min(self):
        n=2
        a1 = array.array('h',[1,2,3,4])
        a2 = array.array('h',[2,1,5,3])
        expected = array.array('h',[1,1,3,3])
        res = iu.array_min(a1,a2)
        self.assertEquals( res, expected)

    def test_mean(self):
        n=2
        a1 = array.array('h',[11,23,15,41])
        a2 = array.array('h',[2,12,50,31])
        expected = array.array('h',[6,17,32,36])
        res = iu.array_mean(a1,a2)
        self.assertEquals( res, expected)
        


if __name__ == "__main__":
    unittest.main()

